---
title: "Advocacy Strategies Analysis using Chi-squared test"
output:
  html_document: default
  html_notebook: default
  pdf_document: default
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. 

## Do different types of legitimacy indicate a difference in strategy frequency?
### Results are in table III at the end
```{r,echo=FALSE}
require(reshape2)

verbose=T
siglevel=0.10
strat_names<-read.table("strategies3.tsv",sep="\t",header = F,stringsAsFactors=FALSE)[1,4:16]
strat_seq<-paste("S",as.character(1:7),sep="")
strat_names2<-data.frame(Type=c(rep("Participatory",7),rep("Transactional",7)),
                         Strategy=c(paste("Participatory",strat_seq,sep=""),
                                    paste("Transactional",strat_seq,sep="")),
                          Name=as.character(c(strat_names[1,1:6],"",strat_names[1,7:13])))
                         
strategies3<-read.table("strategies3.tsv",sep="\t",header=T)
strategies3<-cbind(Use=rep("Yes",3),strategies3)
strategies4<-cbind(Use=rep("No",3),strategies3[,2:4],c(14,9,4)-strategies3[,5:17])
strategies4a<-rbind(strategies3,strategies4)
strategies4b<-cbind(strategies4a[,1:4],Type=rep("Participatory",3),strategies4a[,5:10],rep(2,3))
colnames(strategies4b)[6:12]=strat_seq
strategies4c<-cbind(strategies4a[,1:4],Type=rep("Transactional",3),strategies4a[,11:17])
colnames(strategies4c)[6:12]=strat_seq
strategies5<-melt(rbind(strategies4b,strategies4c))
colnames(strategies5)[5:7]=c("Type","Strategy","Count")
strategies5$Strategy=paste(strategies5$Type,strategies5$Strategy,sep="")
strategies5<-cbind(strategies5,Q1=as.numeric(strategies5$Quad)==1,Q2=as.numeric(strategies5$Quad)==2,Q3=as.numeric(strategies5$Quad)==3)
# There is not participatory strategy S7
#strategies5=strategies5[strategies5$Strategy!="S7" | strategies5$Type!="Participatory", ]
source("3wayindep.R")
results_rowa<-c()
results_rowl<-c()
results_rowd<-c()
results_rowq<-c()
results_rowl2<-c()
for(y in levels(strategies5[,"Type"]))
  for(x in 1:7) {   
    this_strategy=paste(y,"S",as.character(x),sep="")
    print(paste(y,as.character(x),":",strat_names2[strat_names2$Strategy==this_strategy,"Name"]))
    temp.df = xtabs(Count~Quadrant+Use,strategies5[strategies5$Strategy==this_strategy,])
    ft<-f.chisq.test2(temp.df)
    if (verbose) print(ft)
    results_rowa<-c(results_rowa,ifelse(ft$p.value<.1,paste("Yes (p=",format(ft$p.value,digits=2),")",sep=""),"No"))

    temp.df = xtabs(Count~Local+Use,strategies5[strategies5$Strategy==this_strategy,])
    ft<-f.chisq.test2(temp.df)
    if (verbose) print(ft)
    results_rowl<-c(results_rowl,ifelse(ft$p.value<.1,paste("Yes (p=",format(ft$p.value,digits=2),")",sep=""),"No"))

    temp.df = xtabs(Count~Donor+Use,strategies5[strategies5$Strategy==this_strategy,])
    ft<-f.chisq.test2(temp.df)
    if (verbose) print(ft)
    results_rowd<-c(results_rowd,ifelse(ft$p.value<.1,paste("Yes (p=",format(ft$p.value,digits=2),")",sep=""),"No"))
    
    var2=""
    for (z in 1:3) {
      var=paste("Q",as.character(z),sep="")
      formula=paste("Count~",var,"+Use",sep="")
      temp=xtabs(formula,strategies5[strategies5$Strategy==this_strategy,])
      temp2=f.twowayindep(temp,c(var,"Use"),c(1,2),F,siglevel) 
      if (temp2$p.value<siglevel) {
        var2=paste(var2,var," (p=",format(temp2$p.value,digits=2),") ",sep="")
      }
    }
    results_rowq<-c(results_rowq,var2)
  }

```


## table III
```{r,echo=FALSE,results='asis'}
# remove column 7 in results - non existent Participatory S 7
results<-rbind(c("Variable",f.word.wrap(strat_names,15)),c("Quadrant",results_rowa[c(1:6,8:14)]),c("Local",results_rowl[c(1:6,8:14)]),c("Donor",results_rowd[c(1:6,8:14)]),c("Quad",results_rowq[c(1:6,8:14)]))
require(xtable)
print(xtable(results),type="html",include.rownames=F,include.colnames=F)
```

## Test for differences in strategies using marginal sum of strategies used (first column of table IV)
```{r,echo=FALSE}
strat_marg<-xtabs(Count~Quadrant+Use,strategies5)
ft=f.chisq.test2(strat_marg)
if (verbose) print(ft)
cat("Therefore legitimacy varies with the strategies chosen at a significant level\n")
cat("Test for local legitimacy\n")
strat_marg<-xtabs(Count~Local+Use,strategies5)
ft=f.chisq.test2(strat_marg)
if (verbose) print(ft)
cat("Therefore local legitimacy varies with the strategies chosen at a significant level\n")
cat("Test for donor legitimacy\n")
strat_marg<-xtabs(Count~Donor+Use,strategies5)
ft=f.chisq.test2(strat_marg)
if (verbose) print(ft)
cat("Therefore donor legitimacy does NOT vary with the strategies chosen at a significant level. Donor legitimacy and strategy use are marginally independent.\n")
```

## Test for differences in type of strategies given legitimacy
```{r,echo=FALSE}
  temp=xtabs(Count~Type+Quadrant+Use,strategies5)
  vars=c("Type","Quadrant","Use")
  J_quadrant=f.jointindep(temp,vars,c(3,1),2,verbose,siglevel)

  temp=xtabs(Count~Type+Local+Use,strategies5)
  vars=c("Type","Local","Use")
  J_local=f.jointindep(temp,vars,c(3,1),2,verbose,siglevel)


  temp=xtabs(Count~Type+Donor+Use,strategies5)
  vars=c("Type","Donor","Use")
  J_local=f.jointindep(temp,vars,c(3,1),2,verbose,siglevel)

```
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
